package com.yonyou.pmclouds.project.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.organization.mapper.OrganizationMapper;
import com.yonyou.pmclouds.project.entity.ProjectConst;
import com.yonyou.pmclouds.project.mapper.ProCountMapper;
import com.yonyou.pmclouds.project.rmiitf.ProjectCountRemoteService;
import com.yonyou.pmclouds.statistic.entity.ProStateCountResultVO;
import com.yonyou.pmclouds.statistic.entity.ProStateCountVO;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;


@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = ProjectCountRemoteService.class)
public class ProjectCountRemoteServiceImpl implements ProjectCountRemoteService {

    public String STATE_NAME_END = "state_end";
    public String STATE_NAME_GOING = "state_going";
    public String STATE_NAME_CREATED = "state_created";
    public String STATE_NAME_PAUSE = "state_pause";

    @Autowired
    private ProCountMapper proCountMapper;
    @Autowired
    private OrganizationMapper mapper;

    /**
     *  项目状态统计查询
     * @param pkTenant
     * @param pkSrcOrg 这里传的是组织中的pkSrc
     * @param startDate
     * @param endDate
     * @return
     * @throws BusinessException
     */
    @Override
    public ProStateCountResultVO getProStateCountVOs(String pkTenant, String pkSrcOrg, String startDate, String endDate) throws BusinessException{

        ProStateCountVO[] proStateCountVOs = proCountMapper.getProStateCountVOs(pkTenant, pkSrcOrg, startDate, endDate);

        if(ArrayUtils.isEmpty(proStateCountVOs)){
            ProStateCountResultVO proStateCountResultVO = new ProStateCountResultVO();
            proStateCountResultVO.setTotalCount(0);
            proStateCountResultVO.setProStateCountVOs(new ProStateCountVO[0]);
            return proStateCountResultVO;
        }

        byte tempState ;
        int totalCount = 0;
        for (ProStateCountVO proStateCount :
                proStateCountVOs) {
            tempState = proStateCount.getProjectState();
            totalCount = totalCount+proStateCount.getCount();

            switch (tempState){
                case ProjectConst.STATE_END :
                    proStateCount.setStateName(STATE_NAME_END);
                    break;
                case ProjectConst.STATE_GOING :
                    proStateCount.setStateName(STATE_NAME_GOING);
                    break;
                case ProjectConst.STATE_CREATED :
                    proStateCount.setStateName(STATE_NAME_CREATED);
                    break;
                case ProjectConst.STATE_PAUSE :
                    proStateCount.setStateName(STATE_NAME_PAUSE);
                    break;
            }
        }
        ProStateCountResultVO proStateCountResultVO = new ProStateCountResultVO();
        proStateCountResultVO.setProStateCountVOs(proStateCountVOs);
        proStateCountResultVO.setTotalCount(totalCount);

        return proStateCountResultVO;
    }

}
